﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using log4net;

namespace StockFinder.SymbolExtractor
{
    public class SymbolExtractorEngine
    {
        private static readonly ILog _Log = LogManager.GetLogger(typeof(SymbolExtractorEngine));

        const string SYMBOL_PATTERN = "<tt>([\\w\\s\\.]+)</tt>";

        public List<string> ExtractSymbolsFromUrl(string url)
        {
            _Log.DebugFormat("Making page request for {0}...", url);

            List<string> symbols = new List<string>();

            //create webrequest from url
            HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;

            HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;

            string pageSource = null;

            //create the reader to extract the page source
            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                _Log.DebugFormat("Reading page source...");

                //get the page source
                pageSource = streamReader.ReadToEnd();
            }

            webResponse = null;
            webRequest = null;

            MatchCollection matches = Regex.Matches(pageSource, SYMBOL_PATTERN, RegexOptions.Multiline);

            _Log.DebugFormat("Found {0} matches", matches.Count);            

            foreach (Match match in matches)
            {
                if (match.Groups.Count == 2)
                {
                    string symbol = match.Groups[1].Value;

                    symbols.Add(symbol);

                    _Log.DebugFormat("Added {0}", symbol);
                }
            }

            return symbols;
        }
    }
}
